<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
class CurrencyController extends AdminController {
	protected $currency;
	public function _initialize(){
		parent::_initialize();
		$this->currency=M('Currency');
	}
	//空操作
	public function _empty(){
		header("HTTP/1.0 404 Not Found");
		$this->display('Public:404');
	}
	
	/**
	 * 提币记录
	 */
	public function tibi_index(){
		
		$cuid=I('cuid');
		$email=I('email');
		if(!empty($cuid)){
			$where[C("DB_PREFIX")."tibi.currency_id"]=$cuid;
			$this->assign("id",$cuid);
		}
		if(!empty($email)){
			$name=M("Member")->where("email='{$email}'")->find();
			$where[C("DB_PREFIX")."tibi.user_id"]=$name['member_id'];
		}
		$where[C("DB_PREFIX")."tibi.status"]=array("in",array(0,1));
		
		$field=C("DB_PREFIX")."tibi.*,".C("DB_PREFIX")."member.email,".C("DB_PREFIX")."currency.currency_name";
		$count      = M("Tibi")->where($where)->join(C("DB_PREFIX")."member on ".C("DB_PREFIX")."tibi.user_id=".C("DB_PREFIX")."member.member_id")
		->join(C("DB_PREFIX")."currency on ".C("DB_PREFIX")."tibi.currency_id=".C("DB_PREFIX")."currency.currency_id")->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		//给分页传参数
		setPageParameter($Page, array('cuid'=>I("cuid"),'email'=>$name['member_id']));
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性

		$list = M("Tibi")->field($field)->where($where)->join(C("DB_PREFIX")."member on ".C("DB_PREFIX")."tibi.user_id=".C("DB_PREFIX")."member.member_id")
		->join(C("DB_PREFIX")."currency on ".C("DB_PREFIX")."tibi.currency_id=".C("DB_PREFIX")."currency.currency_id")->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		
		
		//读取币种表
		
		$curr=M("Currency")->select();
		$this->assign("curr",$curr);
		
		$this->display();
	}
	
	/**
	 * 充值记录
	 */
	public function chongzhi_index(){
		$cuid=I('cuid');
		$email=I('email');
	
		if(!empty($cuid)){
			$where[C("DB_PREFIX")."tibi.currency_id"]=I("cuid");
			$this->assign("id",I("cuid"));
		}
		if(!empty($cuid)){
			$name=M("Member")->where("email='{$email}'")->find();
			$where[C("DB_PREFIX")."tibi.user_id"]=$name['member_id'];
		}
		$where[C("DB_PREFIX")."tibi.status"]=array("in",array(2,3));
			
		$field=C("DB_PREFIX")."tibi.*,".C("DB_PREFIX")."member.email,".C("DB_PREFIX")."currency.currency_name";
		
		$count      = M("Tibi")
		->where($where)
		->join(C("DB_PREFIX")."member on ".C("DB_PREFIX")."tibi.user_id= ".C("DB_PREFIX")."member.member_id")
		->join(C("DB_PREFIX")."currency on ".C("DB_PREFIX")."tibi.currency_id=".C("DB_PREFIX")."currency.currency_id")->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		//给分页传参数
		setPageParameter($Page, array('cuid'=>$cuid,'email'=>$email));
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = M("Tibi")->field($field)->where($where)->join(C("DB_PREFIX")."member on ".C("DB_PREFIX")."tibi.user_id=".C("DB_PREFIX")."member.member_id")
		->join(C("DB_PREFIX")."currency on ".C("DB_PREFIX")."tibi.currency_id=".C("DB_PREFIX")."currency.currency_id")->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
	
	
		//读取币种表
	
		$curr=M("Currency")->select();
		$this->assign("curr",$curr);
	
		$this->display();
	}
	/**
	 * 新增币种
	 * @author jry <598821125@qq.com>
	 */
	public function add(){	    
	    if(IS_POST){	
	        $_POST['add_time']=time();
	        if(M('Currency')->add($_POST)){
	            $this->success('新增成功', U('index'));
	        }else{
	            $this->error('新增失败', M('Currency')->getError());
	        }
	    }else{
	        $currency_currency=M("Currency")->where("id <> '{$_GET['id']}'")->select();
	        $is_line_text=array('0'=>'未上线','1'=>'已上线');
	        $is_lock_text=array('1'=>'未开启','0'=>'已开启');
	        //使用FormBuilder快速建立表单页面。
	        $builder = new \Common\Builder\FormBuilder();
	        $builder->setMetaTitle('编辑币种') //设置页面标题
	        ->setPostUrl(U('add')) //设置表单提交地址
	        ->addFormItem('id', 'hidden', 'ID', 'ID')
	        ->addFormItem('currency_name', 'text', '货币名称', '货币名称')
	        ->addFormItem('currency_mark', 'text', '英文标识', '英文标识')
	        ->addFormItem('currency_all_tibi','text','最大(单次提币额度)')
	        ->addFormItem('currency_all_num', 'text', '发行总量','与最新价格乘积为总市值')
	        ->addFormItem('currency_digit_num','text','限制位数')
	        ->addFormItem('currency_url','text','链接地址')
	        ->addFormItem('rpc_user','text','用户名')
	        ->addFormItem('rpc_pwd','text','密码')
	        ->addFormItem('rpc_url','text','地址')
	        ->addFormItem('port_number','text','端口号')
	        ->addFormItem('qianbao_key','text','钱包秘钥','密钥妥善保存，一旦丢失，后果严重且找不回')
	        ->addFormItem('qianbao_balance', 'text', '钱包余额', '钱包余额')
	        ->addFormItem('currency_buy_fee', 'text', '买入手续费', '买入手续费')
	        ->addFormItem('currency_sell_fee', 'text', '卖出手续费', '卖出手续费')
	        ->addFormItem('price_up', 'text', '涨停价', '涨停价')
	        ->addFormItem('price_down', 'text', '跌停价', '跌停价')
	        ->addFormItem('sort', 'text', '排序', '排序')
	        ->addFormItem('is_line', 'radio', '上线状态', '上线状态', $is_line_text)
	        ->addFormItem('is_lock', 'radio', '交易状态', '交易状态', $is_lock_text)
	        // ->addFormItem('is_lock', 'select', '买入币种', '买入币种', $currency_currency)
	        ->addFormItem('detail_url', 'text', '货币详情链接', '货币详情链接', $currency_currency)
	        //->setFormData($info)
	        ->display();
	    }
	}
    /**
     * 编辑用户
     * @author jry <598821125@qq.com>
     */
    public function edit($id){    
       
        if(IS_POST){
           
            $_POST['utime']=time();
            
            if(M("Currency")->save($_POST)){
                $this->success('更新成功', U('index'));
            }else{
                $this->error('更新失败', M("Currency")->getError());
            }
        }else{       
            $currency_currency=M("Currency")->where("id <> '{$id}'")->select();          
            $info = M('Currency')->find($id);
            $is_line_text=array('0'=>'未上线','1'=>'已上线');
            $is_lock_text=array('1'=>'未开启','0'=>'已开启');
           //使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('编辑币种') //设置页面标题
                    ->setPostUrl(U('edit')) //设置表单提交地址
                    ->addFormItem('id', 'hidden', 'ID', 'ID')
                    ->addFormItem('currency_name', 'text', '货币名称', '货币名称')
                    ->addFormItem('currency_mark', 'text', '英文标识', '英文标识')
                    
                    ->addFormItem('currency_all_tibi','text','最大(单次提币额度)')
                    ->addFormItem('currency_all_num', 'text', '发行总量','与最新价格乘积为总市值')
                    ->addFormItem('currency_digit_num','text','限制位数')
                    ->addFormItem('currency_url','text','链接地址')
                    ->addFormItem('rpc_user','text','用户名')
                    ->addFormItem('rpc_pwd','text','密码')
                    ->addFormItem('rpc_url','text','地址')
                    ->addFormItem('port_number','text','端口号')
                    ->addFormItem('qianbao_key','text','钱包秘钥','密钥妥善保存，一旦丢失，后果严重且找不回')
                    ->addFormItem('qianbao_balance', 'text', '钱包余额', '钱包余额')
                    ->addFormItem('currency_buy_fee', 'text', '买入手续费', '买入手续费')
                    ->addFormItem('currency_sell_fee', 'text', '卖出手续费', '卖出手续费')
                    ->addFormItem('price_up', 'text', '涨停价', '涨停价')
                    ->addFormItem('price_down', 'text', '跌停价', '跌停价')
                    ->addFormItem('sort', 'text', '排序', '排序')
                    ->addFormItem('is_line', 'radio', '上线状态', '上线状态', $is_line_text)
                    ->addFormItem('is_lock', 'radio', '交易状态', '交易状态', $is_lock_text)
                   // ->addFormItem('is_lock', 'select', '买入币种', '买入币种', $currency_currency)
            ->addFormItem('detail_url', 'text', '货币详情链接', '货币详情链接', $currency_currency)
                    ->setFormData($info)
                    ->display();
         }
     }
	public function index(){
		 //搜索
        $keyword = I('keyword', '', 'string');
        $condition = array('like','%'.$keyword.'%');
        $map['id|currency_name'] = array($condition, $condition,'_multi'=>true);
        //获取所有用户
        $map['status'] = array('egt', '0'); //禁用和正常状态
        $data_list = D('Currency')->page(!empty($_GET["p"])?$_GET["p"]:1, C('ADMIN_PAGE_ROWS'))->where($map)->order('add_time desc,id desc')->select();
        $page = new \Common\Util\Page(D('Currency')->where($map)->count(), C('ADMIN_PAGE_ROWS'));
        $server_center_right[1]['title'] = '取消报单中心';
        $server_center_right[1]['class'] = 'label label-danger ajax-get confirm';
        $server_center_right[1]['href'] = U(MODULE_NAME.'/'.CONTROLLER_NAME.'/setIsServerCenter', array('status'=>'cancel','ids' => '__data_id__'));
        //使用Builder快速建立列表页面。
        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('币种列表') //设置页面标题
                ->addTopButton('addnew')  //添加新增按钮
                ->addTopButton('resume')  //添加启用按钮
                ->addTopButton('forbid')  //添加禁用按钮
                ->addTopButton('delete')  //添加删除按钮
                ->setSearch('请输入ID/用户名/邮箱/手机号', U('index'))
                ->addTableColumn('id', 'ID')
                ->addTableColumn('currency_name', '货币名称')
                ->addTableColumn('is_line', '上线状态','function','is_line_text')
                ->addTableColumn('is_lock', '交易状态','function','is_lock_text')
                ->addTableColumn('currency_all_tibi', '最大(单次提币额度)')
                ->addTableColumn('currency_all_num', '发行总量')
                ->addTableColumn('currency_mark', '英文标识')
                ->addTableColumn('qianbao_balance', '钱包余额')
                ->addTableColumn('currency_buy_fee', '买入手续费')
                ->addTableColumn('currency_sell_fee', '卖出收费')
                ->addTableColumn('price_up', '涨停价')
                ->addTableColumn('price_down', '跌停价')
                ->addTableColumn('utime', '修改时间', 'time')
                //->addTableColumn('reg_type', '注册方式')
                //->addTableColumn('sort', '排序', 'text')
                //->addTableColumn('status', '状态', 'status')
                ->addTableColumn('right_button', '操作', 'btn')
                ->setTableDataList($data_list) //数据列表
                ->setTableDataPage($page->show()) //数据列表分页
                ->addRightButton('edit')   //添加编辑按钮
                //->addRightButton('forbid') //添加禁用/启用按钮
                ->addRightButton('delete') //添加删除按钮
                ->addRightButton('caseButton','',$server_center_right)
                ->display();
    }
    public function shangxian(){
    	if(!empty($_GET['currency_id'])){
    		$rs=M('Currency')->where('currency_id='.$_GET['currency_id'])->setField('is_lock',0);
    	}
    	if($rs){
    		$this->success('操作成功');
    	}else{
    		$this->error('操作失败');
    	}
    }
    //删除的时候，要判断个人账户下有没有钱
    public function del(){
    	if(empty($_GET['currency_id'])){
    		$this->error('删除数据不存在');
    	}
    	$rs=M('Currency_user')->where('currency_id='.$_GET['currency_id'])->sum("num");
    
    	if($rs>0){
    		$this->error('该币种尚有用户持有，不能删除');
    	}
    
    	$list=$this->currency->where('currency_id='.$_GET['currency_id'])->delete();
    	M('Currency_user')->where('currency_id='.$_GET['currency_id'])->delete();
    	if($list){
    		$this->success('删除成功');
    	}else{
    		$this->error('删除失败');
    	}
    }
    //删除对应的币种详情图片
    public function delCurrencyPic(){
    	$id=I('get.id');
    	$list=M('Currency_pic')->where('id='.$id)->delete();
    	if($list){
    		$this->success('删除成功');
    	}else{
    		$this->error('删除失败');
    	}
    }
    
 
    
    /**
     * 给某个用户钱包转账
     */
    public function  set_member_currencyForQianbao(){
    	$cuid=intval(I("cuid"));
    	if(empty($cuid)){
    		$this->error("无效货币参数",U("Currency/index"));exit();
    	}
    	$currency=M("Currency")->where("currency_id='$cuid'")->find();
    	
    	//$currency['balance']=$this->get_qianbao_balance($currency);
    
    	if(empty($currency)){
    		$this->error("无效货币",U("Currency/index"));exit();
    	}
    	if(IS_POST){
    	
    		$admin=M("Admin")->where("admin_id='{$_SESSION['admin_userid']}'")->find();
    		if(empty($_POST['password'])){
    			$this->error("请输入管理员密码");
    		}
    		if(md5($_POST['password'])!=$admin['password']){
    			$this->error("您输入的管理员密码错误");
    		}
    		
    		$username=I("name");//用户名
    		$num=I('num');//数量
    		if(empty($username)){
    			$this->error("请输入用户名");exit();
    		}
    		if(empty($num)||!is_numeric($num)){
    			$this->error("数量请输入数字类型");exit();
    		}
    		$member=M("Member")->where("email='$username'")->find();
    		if(empty($member)){
    			$this->error("查无此人，请核实");exit();
    		}
    		
    		$qa=M("Qianbao_address")->where("user_id='{$member['member_id']}' and currency_id='{$cuid}'")->find();
    		if(empty($qa['qianbao_url'])){
    			$this->error("此用户没有绑定提币地址，无法转账");exit();
    		}
    		//判断看这个钱包地址是否是真实地址
    		if(!$this->check_qianbao_address($qa['qianbao_url'],$currency)){
    		$this->error("提币地址不是一个有效地址");exit();
    		}
    		$num=floatval($num);
    		$data['fee']=0;//手续费
    		$data['currency_id']=$cuid;
    		$data['user_id']=$qa['user_id'];
    		$data['url']=$qa['qianbao_url'];
    		$data['name']=$qa['name'];
    		$data['num']=$num;
    		$data['actual']=$num;//实际到账价格
    		$data['status']=0;
    		$data['add_time']=time();
    		
    		$tibi=$this->qianbao_tibi($qa['qianbao_url'],$num,$currency);//提币程序
    		
    		if($tibi){//成功写入数据库
    			$data['ti_id']=$tibi;
    			$re=M("Tibi")->add($data);
    			//减钱操作
//     			M("Currency_user")->where("member_id='{$_SESSION['USER_KEY_ID']}' and currency_id='$cuid'")->setDec("num",$num);
    			$this->success("转账成功，请耐心等待",U('Currency/index'));exit();
    		
    		}else{//失败提示
    			$this->error("转账失败");exit();
    		}
    	}
    	
    	$this->assign("currency",$currency);
    	$this->display();
    }
    
    /**
     * 检测地址是否是有效地址
     *
     * @return boolean 如果成功返回个true
     * @return boolean 如果失败返回个false；
     *  @param unknown $url
     *  @param $port_number 端口号 来区分不同的钱包
     */
     
    private function check_qianbao_address($url,$currency){
    	require_once 'App/Common/Common/easybitcoin.php';
    	$bitcoin = new \Bitcoin($currency['rpc_user'],$currency['rpc_pwd'],$currency['rpc_url'],$currency['port_number']);
    	$address = $bitcoin->validateaddress($url);
    	if($address['isvalid']){
    		return true;
    	}else{
    		return false;
    	}
    }
    /**
     * 提币引用的方法
     * @param unknown $url 钱包地址
     * @param unknown $money 提币数量
     */
    private function qianbao_tibi($url,$money,$currency){
     	require_once 'App/Common/Common/easybitcoin.php';
    $bitcoin = new \Bitcoin($currency['rpc_user'],$currency['rpc_pwd'],$currency['rpc_url'],$currency['port_number']);
    $bitcoin->walletlock();//强制上锁
    $bitcoin->walletpassphrase($currency['qianbao_key'],20);
    $id=$bitcoin->sendtoaddress($url,$money);
    $bitcoin->walletlock();
    	return $id;
    }
    /**
     * 修改涨停跌停价格
     */
    public function savePrice(){
    	$currency_id = trim(I('id','','intval'));
    	if(empty($currency_id)){
    		$data['status'] = -1;
    		$data['info'] = '参数错误';
    		$this->ajaxReturn($data);
    	}
    	if(!is_numeric($_POST['price_up']) || !is_numeric($_POST['price_down'])){
    		$data['status'] = -2;
    		$data['info'] = '填写必须是数字';
    		$this->ajaxReturn($data);
    	}
    	$r[] = M('Currency')->where("currency_id = {$currency_id}")->setField('price_up',$_POST['price_up']);
    	$r[] = M('Currency')->where("currency_id = {$currency_id}")->setField('price_down',$_POST['price_down']);
    	if(in_array($r, false)){
    		$data['status'] = -3;
    		$data['info'] = '服务器繁忙,请稍后重试';
    		$this->ajaxReturn($data);
    	}else{
    		$data['status'] = 1;
    		$data['info'] = '修改成功';
    		$this->ajaxReturn($data);
    	}
    }
    
}